home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / midi / gfft.lha / gfft-2.03 / source / gfft-2.03-source.lha / okread.c < prev    next >
C/C++ Source or Header  |  1996-01-02  |  7KB  |  352 lines

  1. /***************************************************************************
  2.  *          Copyright (C) 1994  Charles P. Peterson                  *
  3.  *         4007 Enchanted Sun, San Antonio, Texas 78244-1254             *
  4.  *              Email: Charles_P_Peterson@fcircus.sat.tx.us                *
  5.  *                                                                         *
  6.  *          This is free software with NO WARRANTY.                  *
  7.  *          See gfft.c, or run program itself, for details.              *
  8.  *              Support is available for a fee.                      *
  9.  ***************************************************************************
  10.  *
  11.  * Program:     gfft--General FFT analysis
  12.  * File:        okread.c
  13.  * Purpose:     OK! Read the data
  14.  * Author:      Charles Peterson (CPP)
  15.  * History:     24-July-1993 CPP; Created.
  16.  *              6-Jan-95 CPP (1.31); add progress indicator and check stop
  17.  * Comment:
  18.  */
  19.  
  20. #define PROGRESS_INCREMENT 2048
  21.  
  22. #include <stdio.h>
  23. #include <string.h>
  24.  
  25. #ifdef _AMIGA
  26. #ifdef _M68881
  27. #include <m68881.h>
  28. #endif
  29. #endif
  30.  
  31. #include "gfft.h"
  32. #include "settings.h"
  33. #include "wbench.h"
  34.  
  35. static unsigned long save_data_entered (void);
  36.  
  37. static unsigned long save_data_index = 0;
  38.  
  39. static unsigned long started = FALSE;
  40.  
  41. static unsigned long current_frame_count = 0;
  42.  
  43. unsigned long ok_read (float *indata, unsigned long number_samples)
  44. {
  45.     static unsigned long save_data_length = 0;
  46.     unsigned long i;
  47.     int j;
  48.  
  49.     union {
  50.     unsigned short u;
  51.     signed short s;
  52.     } word;
  53.  
  54.     union {
  55.     unsigned char u;
  56.     signed char s;
  57.     } byte;
  58. /*
  59.  * If indata pointer is NULL, we're just scanning to find file length
  60.  * Rewind file and return when done
  61.  */
  62.     if (!indata)
  63.     {
  64.     if (OkFrames != ULONG_MAX)  /* Already known or specified */
  65.     {
  66.         return OkFrames;
  67.     }
  68.     if (ReadPtr != stdin)
  69.     {
  70.         error_message (SCANNING_FILE);  /* This might take some time */
  71.         for (i = 0; i < OkOffset; i++)
  72.         {
  73.         if (!fread (&byte, sizeof byte, 1, ReadPtr))
  74.         {
  75.             return 0L;
  76.         }
  77.         }
  78.         if (InputFormat.bits <= 8)
  79.         {
  80.         for (i = 0; fread (&byte, sizeof byte, 1, ReadPtr); i++);
  81.         }
  82.         else
  83.         {
  84.         for (i = 0; fread (&word, sizeof word, 1, ReadPtr); i++);
  85.         }
  86.         rewind (ReadPtr);
  87.         i /= OkChannels;
  88.         FileFrames = i;  /* This value is now known */
  89.         if (i >= StartFrame)
  90.         {
  91.         i -= StartFrame;
  92.         }
  93.         else
  94.         {
  95.         i = 0L;
  96.         }
  97.         return i;
  98.     }
  99.     else
  100.     {
  101.         if (!Save_Data)
  102.         {
  103.         save_data_length = save_data_entered ();
  104.         }
  105.         return save_data_length;
  106.     }
  107.     }
  108.  
  109. /*
  110.  * An actual file pointer has been provided; read from file
  111.  */
  112.     if (ReadPtr != stdin)
  113.     {
  114.     if (!started)
  115.     {
  116.     /*
  117.      * Skip over start bytes
  118.      */
  119.         for (i = 0L; i < OkOffset; i++)
  120.         {
  121.         if (!fread (&byte, sizeof byte, 1, ReadPtr))
  122.         {
  123.             return 0L;
  124.         }
  125.         }
  126.         /*
  127.          * Skip over start frames
  128.      */
  129.         if (InputFormat.bits <= 8)
  130.         {
  131.         ULONG start_samples = OkChannels * OkStartFrame;
  132.         for (i = 0L; i < start_samples; i++)
  133.         {
  134.             if (!fread (&byte, sizeof byte, 1, ReadPtr))
  135.             {
  136.             return 0L;
  137.             }
  138.         }
  139.         }
  140.         else  /* InputFormat.bits > 8 */
  141.         {
  142.         ULONG start_samples = OkChannels * OkStartFrame;
  143.         for (i = 0L; i < start_samples; i++)
  144.         {
  145.             if (!fread (&word, sizeof word, 1, ReadPtr))
  146.             {
  147.             return 0L;
  148.             }
  149.         }
  150.         }
  151.         started = TRUE;
  152.     }
  153.     /*
  154.      * Here is the main read loop
  155.      */
  156.     for (i = 0L; i < number_samples; i++)
  157.     {
  158.         if (i % PROGRESS_INCREMENT == 0)  
  159.           progress_requester__check_stop ();
  160.         if (++current_frame_count > OkFrames)
  161.         {
  162.         break;
  163.         }
  164.         if (Channel > 1)
  165.         {
  166.         for (j = 1; j < Channel; j++)
  167.         {
  168.             if (InputFormat.bits <= 8)
  169.             {
  170.             if (!fread (&byte, sizeof byte, 1, ReadPtr))
  171.             {
  172.                 return i;
  173.             }
  174.             }
  175.             else
  176.             {
  177.             if (!fread (&word, sizeof word, 1, ReadPtr))
  178.             {
  179.                 return i;
  180.             }
  181.             }
  182.         }                          /* end for (j...) */
  183.         }                              /* end if Channel > 1 */
  184.         if (InputFormat.bits <= 8)
  185.         {
  186.         if (!fread (&byte, sizeof byte, 1, ReadPtr))
  187.         {
  188.             break;
  189.         }
  190.         if (!InputFormat.zero)
  191.         {
  192.             for (j = InputFormat.bits; j < 8; j++)
  193.             {
  194.             byte.s /= 2;
  195.             }
  196.             *indata++ = byte.s;
  197.         }
  198.         else
  199.         {
  200.             for (j = InputFormat.bits; j < 8; j++)
  201.             {
  202.             byte.u /= 2;
  203.             }
  204.             if (byte.u >= InputFormat.zero)
  205.             {
  206.             *indata++ = byte.u - InputFormat.zero;
  207.             }
  208.             else
  209.             {
  210.             *indata = InputFormat.zero - byte.u;
  211.             *indata = (- (*indata));
  212.             indata++;
  213.             }
  214.         }
  215.         }
  216.         else /* Sizes greater than 16 bits not currently supported */
  217.         {
  218.         if (!fread (&word, sizeof word, 1, ReadPtr))
  219.         {
  220.             break;
  221.         }
  222.         if (!InputFormat.zero)
  223.         {
  224.             for (j = InputFormat.bits; j < 16; j++)
  225.             {
  226.             word.s /= 2;
  227.             }
  228.             *indata++ = word.s;
  229.         }
  230.         else
  231.         {
  232.             for (j = InputFormat.bits; j < 16; j++)
  233.             {
  234.             word.u /= 2;
  235.             }
  236.             if (word.u >= InputFormat.zero)
  237.             {
  238.             *indata++ = word.u - InputFormat.zero;
  239.             }
  240.             else
  241.             {
  242.             *indata = InputFormat.zero - word.u;
  243.             *indata = (- (*indata));
  244.             indata++;
  245.             }
  246.         }
  247.         }
  248.         if (Parseval)
  249.         {
  250.         Sample_Sum_Of_Squares += indata[-1] * indata[-1];
  251.         }
  252.         Sample_Frame_Count++;
  253.         if (Channel < OkChannels)
  254.         {
  255.         for (j = Channel; j < OkChannels; j++)
  256.         {
  257.             if (InputFormat.bits <= 8)
  258.             {
  259.             if (!fread (&byte, sizeof byte, 1, ReadPtr))
  260.             {
  261.                 return i;
  262.             }
  263.             }
  264.             else
  265.             {
  266.             if (!fread (&word, sizeof word, 1, ReadPtr))
  267.             {
  268.                 return i;
  269.             }
  270.             }
  271.         }                          /* end for j */
  272.         }                              /* end if Channel... */
  273.     }                                  /* end for current_frame_count */
  274.     }                                      /* end if ReadPtr != stdin */
  275.     else
  276. /*
  277.  * Interactive input
  278.  * Read data from terminal into buffer
  279.  * then output as demanded
  280.  */
  281.     {
  282.     if (!Save_Data)
  283.     {
  284.         save_data_length = save_data_entered ();
  285.     }
  286.     for (i = 0L; i < number_samples; i++)
  287.     {
  288.         if (i + save_data_index >= save_data_length) 
  289.         {
  290.         break;
  291.         }
  292.         *indata++ = Save_Data[i + save_data_index];
  293.         if (Parseval)
  294.         {
  295.         Sample_Sum_Of_Squares += indata[-1] * indata[-1];
  296.         }
  297.         Sample_Frame_Count++;
  298.     }
  299.     save_data_index += i;
  300.     }
  301.     return i;
  302. }
  303.     
  304. static unsigned long save_data_entered (void)
  305. {
  306.     unsigned long allocated_length = 1;
  307.     unsigned long i;
  308.     char inputline[1024];
  309.     float inputf;
  310.  
  311.     Save_Data = gmalloc (allocated_length * sizeof (float), 
  312.              NOTHING_SPECIAL);
  313.     for (i = 0L; TRUE; i++)
  314.     {
  315.     printf ("Enter <value> (float OK) or <newline> to end input [%i]: ",i);
  316.     gets (inputline);
  317.     if (strlen (inputline) == 0) 
  318.     {
  319.         break;
  320.     }
  321.     if (1 != sscanf (inputline,"%g",&inputf))
  322.     {
  323.         error_message (INVALID_NUMBER);
  324.         i--;
  325.         continue;
  326.     }
  327.     if (i >= allocated_length)
  328.     {
  329.         Save_Data = grealloc (Save_Data, (i+1) * 
  330.                   sizeof (float), NOTHING_SPECIAL);
  331.         allocated_length = i+1;
  332.     }
  333.     Save_Data[i] = inputf;
  334.     }
  335.     return i;
  336. }
  337.  
  338.  
  339. void ok_rewind (void)
  340. {
  341.     started = FALSE;
  342.     current_frame_count = 0;
  343.     if (ReadPtr != stdin)
  344.     {
  345.     rewind (ReadPtr);
  346.     }
  347.     else
  348.     {
  349.     save_data_index = 0;
  350.     }
  351. }
  352.